package com.jhx.mall.app.servlet;

import com.jhx.mall.app.service.DtsBrandService;
import com.jhx.mall.app.service.DtsCateDetailService;
import com.jhx.mall.app.service.DtsGoodsService;
import com.jhx.mall.app.service.DtsKeywordService;

import com.jhx.mall.app.service.impl.DtsBrandServiceImpl;
import com.jhx.mall.app.service.impl.DtsCateDetailServiceImpl;
import com.jhx.mall.app.service.impl.DtsGoodsServiceImpl;
import com.jhx.mall.app.service.impl.DtsKeywordServiceImpl;

import com.jhx.mall.dao.DtsKeywordDao;
import com.jhx.mall.entity.DtsCategory;
import com.jhx.mall.entity.DtsGoods;
import com.jhx.mall.util.BaseServlet;
import com.jhx.mall.vo.DtsCategoryVo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "DtsGoods", value = "/api/index/goods")
public class DtsGoodsServlet extends BaseServlet {
    private DtsGoodsService service = null;

    private DtsKeywordService keywordService = null;

    private DtsCateDetailService dtscateDetailService = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        service = new DtsGoodsServiceImpl();
        keywordService = new DtsKeywordServiceImpl();
        dtscateDetailService = new DtsCateDetailServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        if (opr.equals("product")) {    // 新品首发
            List<DtsGoods> dtsGoodsList = service.selectGoodsNew();
            if (dtsGoodsList != null){
                ok(request,response,dtsGoodsList);
            } else {
                error(request,response,dtsGoodsList);
            }
        } else if (opr.equals("popular")) {     // 人气推荐
            List<DtsGoods> dtsGoodsList = service.selectGoodsHot();
            if (dtsGoodsList != null){
                ok(request,response,dtsGoodsList);
            } else {
                error(request,response,dtsGoodsList);
            }
        } else if (opr.equals("goodsDetail")) {  // 具体商品
            long id = Long.parseLong(request.getParameter("id"));
            DtsGoods dtsGoods = service.selectGoodsById(id);
            if (dtsGoods != null){
                ok(request,response,dtsGoods);
            } else {
                error(request,response,dtsGoods);
            }

        }else if (opr.equals("cateDetailGoodsList")) {
            long id = Long.parseLong(request.getParameter("id"));
            List<DtsGoods> dtsGoodsList = service.selectGoodsByCateId(id);

        } else if (opr.equals("search")) {
            String keywords = request.getParameter("keywords");
            String goodsName = request.getParameter("goodsName");
            long categoryId = Long.parseLong(request.getParameter("categoryId"));
            long orderPrice = Long.parseLong(request.getParameter("orderPrice"));
            DtsGoods dtsGoods = new DtsGoods();
            dtsGoods.setKeywords(keywords);
            dtsGoods.setCategoryId(categoryId);
            int isKN = keywordService.selectKeywordByKeyword(goodsName);
            if (isKN >= 1) {
                dtsGoods.setKeywords(goodsName);
            } else {
                dtsGoods.setName(goodsName);
            }
            List<DtsGoods> dtsGoodsList = service.selectGoodsByDtsGoods(dtsGoods,orderPrice);
            if (dtsGoodsList != null){
                ok(request,response,dtsGoodsList);
            } else {
                error(request,response,dtsGoodsList);
            }

        } else if (opr.equals("categoryGoods")) {
            List<DtsCategory> dtsCategoryList = dtscateDetailService.getCateDetail();
            List<DtsCategoryVo> dtsCategoryVoList = new ArrayList<>();
            for (DtsCategory dtsCategory: dtsCategoryList) {
                DtsCategoryVo dtsCategoryVo = service.selectGoodsByCategoryVo(dtsCategory);
                dtsCategoryVoList.add(dtsCategoryVo);
            }
            if (dtsCategoryVoList != null){
                ok(request,response,dtsCategoryVoList);
            } else {
                error(request,response,dtsCategoryVoList);
            }
        }
    }
}
